package com.ysqcould.activity.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.hutool.json.JSONObject;
import com.ysqcould.activity.domain.vo.CustomVo;
import com.ysqcould.allocation.domain.WxConfig;
import com.ysqcould.allocation.mapper.WxConfigMapper;
import com.ysqcould.allocation.service.IWxConfigService;
import com.ysqcould.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ysqcould.activity.mapper.ActivityCustomMapper;
import com.ysqcould.activity.domain.ActivityCustom;
import com.ysqcould.activity.service.IActivityCustomService;

/**
 * 活动自定义字段Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-08-14
 */
@Service
public class ActivityCustomServiceImpl implements IActivityCustomService 
{
    @Autowired
    private ActivityCustomMapper activityCustomMapper;

    @Autowired
    private WxConfigMapper wxConfigMapper;

    /**
     * 查询活动自定义字段
     * 
     * @param id 活动自定义字段主键
     * @return 活动自定义字段
     */
    @Override
    public ActivityCustom selectActivityCustomById(Long id)
    {
        return activityCustomMapper.selectActivityCustomById(id);
    }

    /**
     * 查询活动自定义字段列表
     * 
     * @param activityCustom 活动自定义字段
     * @return 活动自定义字段
     */
    @Override
    public JSONObject selectActivityCustomList(ActivityCustom activityCustom)
    {
        JSONObject json=new JSONObject();
        if (activityCustom.getAsId()==1){
            activityCustom.setAsId(2L);
        }
        WxConfig wxConfig = wxConfigMapper.selectWxConfigByAsId(activityCustom.getSourceType(), activityCustom.getAsId());
        //个人入会申请
        if (activityCustom.getSourceType()==2){
            json.set("status",wxConfig.getPersonalStatus());
            final String[] split = wxConfig.getFileUrlOne().split(";");
            for (int i=0;i< split.length;i++) {
                final String fileName = split[i].substring(split[i].lastIndexOf("/") + 1);
                split[i]=fileName;
            }
            json.set("memberRemark",wxConfig.getMemberRemark());
            json.set("fileUrlOne",split);
        }
        //团体入会申请
        if (activityCustom.getSourceType()==4){
            json.set("status",wxConfig.getPersonalStatus());
            final String[] split = wxConfig.getFileUrlThree().split(";");
            for (int i=0;i< split.length;i++) {
                final String fileName = split[i].substring(split[i].lastIndexOf("/") + 1);
                split[i]=fileName;
            }
            json.set("groupRemark",wxConfig.getGroupRemark());
            json.set("fileUrlThree",split);
        }
        //企业入会申请
        if (activityCustom.getSourceType()==3){
            json.set("status",wxConfig.getPersonalStatus());
            final String[] split = wxConfig.getFileUrlTwo().split(";");
            for (int i=0;i< split.length;i++) {
                final String fileName = split[i].substring(split[i].lastIndexOf("/") + 1);
                split[i]=fileName;
            }
            json.set("fileUrlTwo",split);
            json.set("companyRemark",wxConfig.getCompanyRemark());
        }
        List<CustomVo> customs = activityCustomMapper.selectActivityCustomList(activityCustom);
        customs.forEach(p->{
            if (p.getType().equals("radio")||p.getType().equals("picker")||p.getType().equals("checkbox")){
                final String all = p.getData().replaceAll("\\[", "");
                final String s = all.replaceAll("]", "");
                final String str = s.replaceAll("\\\"", "");
                final String st = str.replaceAll("\\\'", "");
                final String[] split = st.split(",");
                p.setDataList(split);
            }
        });
        json.set("list",customs);
        return json;
    }

    /**
     * 新增活动自定义字段
     * 
     * @param activityCustom 活动自定义字段
     * @return 结果
     */
    @Override
    public int insertActivityCustom(ActivityCustom activityCustom)
    {
        activityCustom.setCreateTime(DateUtils.getNowDate());
        return activityCustomMapper.insertActivityCustom(activityCustom);
    }

    /**
     * 修改活动自定义字段
     * 
     * @param activityCustom 活动自定义字段
     * @return 结果
     */
    @Override
    public int updateActivityCustom(ActivityCustom activityCustom)
    {
        activityCustom.setUpdateTime(DateUtils.getNowDate());
        return activityCustomMapper.updateActivityCustom(activityCustom);
    }

    /**
     * 批量删除活动自定义字段
     * 
     * @param ids 需要删除的活动自定义字段主键
     * @return 结果
     */
    @Override
    public int deleteActivityCustomByIds(Long[] ids)
    {
        return activityCustomMapper.deleteActivityCustomByIds(ids);
    }

    /**
     * 删除活动自定义字段信息
     * 
     * @param id 活动自定义字段主键
     * @return 结果
     */
    @Override
    public int deleteActivityCustomById(Long id)
    {
        return activityCustomMapper.deleteActivityCustomById(id);
    }
}
